

from PyQt5 import QtCore
from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtWidgets import QLabel, QWidget
import random

class MyQLabel(QLabel):
    # 自定义信号, 注意信号必须为类属性
    button_clicked_signal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(MyQLabel, self).__init__(parent)

    def mouseReleaseEvent(self, QMouseEvent):
        self.button_clicked_signal.emit()
        
    # 可在外部与槽函数连接
    def connect_customized_slot(self, func):
        self.button_clicked_signal.connect(func)





class CBilibili(object):

    static_contents = []
    def get_contentlist(self):
        return CBilibili.static_contents,len(CBilibili.static_contents)
    def set_contentlist(self,contents):
        CBilibili.static_contents = contents

    stage = None
    def __init__(self, qlabel: QLabel):

        try:
            with open( "res/barrage_content.txt",'r',encoding='utf-8' ) as fp:
                arr = []
                for l in fp.readlines():
                    arr.append(l)
                self.set_contentlist(arr)
        except:
            fp = None
            print(">>> 弹幕内容为空 请检查 res目录下 barrage_content.txt是否为空")
            return
        finally:
            if fp != None:
                fp.close()

         
        self.labels = []
        self.propertys = []
        self.labels.append(qlabel)

        self.stage = stage = qlabel.parent()
        mainWindow = qlabel.parent().parent()
        w = mainWindow.width()
        h = mainWindow.height()
        qlabel.move(w + qlabel.width(),qlabel.y())
        for i in range(64):
            l = MyQLabel(stage)
            l.setAlignment(QtCore.Qt.AlignBottom)
            self.getBilibili(l)
            self.randomBg(l)
            l.move(
                w + random.randint(0, w),
                random.randint(40, h - 40))
            self.labels.append(l)
            self.propertys.append(random.randint(3, 10))
            l.connect_customized_slot(lambda i = i: self.showflytext(i))

        self.timer = QTimer(qlabel.parent())
        self.timer.timeout.connect(self.update)

    def show(self):
        self.timer.start(16)

    def hide(self):
        for l in self.labels:
            l.setVisible(False)
        self.timer.stop()


    def showflytext(self,i):
        print("click barrage content : %s" % self.labels[i].text())


    def getBilibili(self, l: QLabel):
        contents,_len = self.get_contentlist()
        l.setText(contents[random.randint(0,_len-1)])
    
    def randomBg(self,l:QLabel):
        l.setStyleSheet("color: rgb(255, 255, 255);background-color: rgba(%d, %d, %d, 180);"%(random.randint(0,255),random.randint(0,255),random.randint(0,255)))

    def update(self):

        for i in range(64):
            l = self.labels[i]
            speed = self.propertys[i]
            if l.x() < -l.width():
                l.move(l.parent().width() + random.randint(0, l.parent().width()),
                       random.randint(40, l.parent().height() - 40))
                self.propertys[i] = random.randint(3, 10)
                self.getBilibili(l)
                self.randomBg(l)

            l.move(l.x() - speed, l.y())
        
